package com.kaily.imagelib.utils;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.Gravity;

import java.text.DecimalFormat;
import java.util.Calendar;

/**
 * 图片处理工具类
 *
 * @author kaily_zhou
 * @date 2018/10/22 上午10:09
 */
public class ImageUtils {
    public static final String CUSTOMER_NAME_PREFIX = "我是";
    public static final String EXPRESSION_QUTE_PREFIX = "— ";
    private static final int FONT_SIZE_STANDARD = 1;
    private static int gravitys[] = new int[]{Gravity.LEFT, Gravity.CENTER_HORIZONTAL, Gravity.RIGHT};

    /**
     * 解析对其方式
     *
     * @param align
     * @return
     */
    public static int parseAlign(int align) {
        int index = align - 1;
        index = ((index < 0 || index > gravitys.length - 1) ? 0 : index);
        return gravitys[index];
    }

    /**
     * 解析对其字体
     *
     * @param fontSize
     * @return
     */
    public static int parseFontSize(int fontSize) {
        return fontSize * FONT_SIZE_STANDARD;
    }

    /**
     * 获取时间年月日
     *
     * @return
     */
    public static String[] getDateTime() {
        try {
            DecimalFormat df = new DecimalFormat("00");
            Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH) + 1;
            int day = c.get(Calendar.DAY_OF_MONTH);
            return new String[]{String.valueOf(year), df.format(month), df.format(day)};
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 绘制文本内容
     *
     * @param text       文本内容
     * @param paint      画笔
     * @param canvas     画布
     * @param coordinate 坐标位置
     * @param angle      角度
     */
    public static void drawText(String text, Paint paint, Canvas canvas, float[] coordinate, float angle) {
        if (!TextUtils.isEmpty(text) && text.length() > 0
                && paint != null && canvas != null
                && coordinate != null && coordinate.length >= 2) {
            float y = coordinate[1];
            if (angle != 0) {
                canvas.rotate(angle, coordinate[0], y);
            }
            Paint.FontMetrics fontMetrics = paint.getFontMetrics();
            coordinate[1] += Math.abs(fontMetrics.ascent);
            canvas.drawText(text, coordinate[0], coordinate[1], paint);
//            coordinate[1] += Math.abs(fontMetrics.descent);
            if (angle != 0) {
                canvas.rotate(-angle, coordinate[0], y);
            }
        }
    }

    /**
     * 获取文本的宽度
     *
     * @param text
     * @param paint
     * @return
     */
    public static int getTextWidth(String text, Paint paint) {
        int textWidth = 0;
        if (!TextUtils.isEmpty(text) && text.length() > 0 && paint != null) {
            textWidth = (int) paint.measureText(String.valueOf(text));
        }
        return textWidth;
    }

    /**
     * 获取文本的高度
     *
     * @param paint
     * @return
     */
    public static int getTextHeight(Paint paint) {
        int textHeight = 0;
        if (paint != null) {
            Paint.FontMetrics fontMetrics = paint.getFontMetrics();
            textHeight = (int) (fontMetrics.descent - fontMetrics.ascent);
//            textHeight = (int) (fontMetrics.descent);
        }
        return textHeight;
    }

    /**
     * 获取文本Ascent
     *
     * @param paint
     * @return
     */
    public static int getTextAscent(Paint paint) {
        int textHeight = 0;
        if (paint != null) {
            Paint.FontMetrics fontMetrics = paint.getFontMetrics();
            textHeight = Math.abs((int) fontMetrics.ascent);
        }
        return textHeight;
    }
}
